package com.sctvcloud.bazhong.ui.adapter.holder;

import android.content.Context;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.support.v7.widget.DefaultItemAnimator;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.util.Pair;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.Toast;

import com.gridsum.tracker.GridsumWebDissector;
import com.ruihang.generalibrary.ui.util.OnItemInternalClick;
import com.ruihang.generalibrary.ui.widget.CustomEXImageView;
import com.ruihang.generalibrary.ui.widget.CustomFontTextView;
import com.ruihang.generalibrary.ui.widget.TextViewExpandableAnimation;
import com.ruihang.generalibrary.utils.DateUtils;
import com.ruihang.generalibrary.utils.ListUtils;
import com.ruihang.generalibrary.utils.UrlUtils;
import com.sctvcloud.bazhong.R;
import com.sctvcloud.bazhong.application.Constances;
import com.sctvcloud.bazhong.base.BaseActivity;
import com.sctvcloud.bazhong.beans.ADynamicsItem;
import com.sctvcloud.bazhong.beans.ContentImage;
import com.sctvcloud.bazhong.beans.FComment;
import com.sctvcloud.bazhong.beans.HomeListItem;
import com.sctvcloud.bazhong.beans.ImageUr;
import com.sctvcloud.bazhong.beans.NewsItem;
import com.sctvcloud.bazhong.ui.activities.VideoActivity;
import com.sctvcloud.bazhong.ui.adapter.AnchorHomePageAdapter;
import com.sctvcloud.bazhong.ui.adapter.HomeHotVideoContentAdapter;
import com.sctvcloud.bazhong.ui.adapter.HomePicAdapter;
import com.sctvcloud.bazhong.ui.adapter.PicAdapter;
import com.sctvcloud.bazhong.ui.utils.GlideCircleTransform;
import com.sctvcloud.bazhong.ui.utils.GlideUtil;
import com.sctvcloud.bazhong.ui.utils.SkipUtil;
import com.sctvcloud.bazhong.ui.utils.UiListMarginUtils;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class HomeAnchorHolder extends BaseAbsHolder<ADynamicsItem> {
    @BindView(R.id.item_anchor_home_dynamics_img)
    protected ImageView img;
    @BindView(R.id.item_anchor_dynamics_home_name)
    protected CustomFontTextView name;
    @BindView(R.id.item_anchor_dynamics_home_time)
    protected CustomFontTextView time;
    @BindView(R.id.item_anchor_dynamics_home_more)
    protected CustomFontTextView more;
    @BindView(R.id.item_anchor_dynamic_home_pic)
    protected RecyclerView pic_list;
    @BindView(R.id.item_anchor_home_dynamics_video_rl)
    protected ViewGroup video_rl;
    @BindView(R.id.item_anchor_intro)
    protected CustomFontTextView intro;
    @BindView(R.id.item_anchor_home_dynamics_video_coverImg)
    protected ImageView coverImage;
    @BindView(R.id.item_anchor_home_dynamics_video_play)
    protected ImageView play;
    private GridLayoutManager manager;
    private HomePicAdapter picAdapter;
    private ADynamicsItem dyTemp;
    private ArrayList<ContentImage> contentImage;
    private String homePageUrl, title, anchorId;
    private OnAnchorSelectMoreListener listener;
    private OnAnchorSelectItemListener itemListener;

    public HomeAnchorHolder(Context context, View itemView, OnAnchorSelectMoreListener listener,OnAnchorSelectItemListener li) {
        super(context, itemView);
        this.listener = listener;
        itemListener = li;
    }

    //    @Override
//    public void setData(HomeListItem newsItem) {
//        this.homeListItem = newsItem;
//        init(newsItem.getNewsItemList());
//    }
    @Override
    public void setData(ADynamicsItem aDynamicsItem) {
        dyTemp = aDynamicsItem;
        name.setText(!TextUtils.isEmpty(aDynamicsItem.getAnchorName()) ? aDynamicsItem.getAnchorName() : "");
        time.setText(!TextUtils.isEmpty(aDynamicsItem.getPubTime()) ? DateUtils.timeAgo(DateUtils.getDataDate(aDynamicsItem)) : "");
        intro.setText(!TextUtils.isEmpty(aDynamicsItem.getContentText()) ? aDynamicsItem.getContentText() : "");
        title = aDynamicsItem.getAnchorName();
        homePageUrl = aDynamicsItem.getAnchorInfoUrl();
        anchorId = aDynamicsItem.getAnchorId();
        if (aDynamicsItem.isSticky()) {
            itemView.setTag(AnchorHomePageAdapter.FIRST_STICKY_VIEW);

        } else {
            itemView.setTag(AnchorHomePageAdapter.NONE_STICKY_VIEW);
        }


        if (!TextUtils.isEmpty(aDynamicsItem.getAnchorAvatar())) {
            GlideUtil.getGlid(context, aDynamicsItem.getAnchorAvatar()).error(R.mipmap.icon_circle_avatar)
                    .placeholder(R.mipmap.icon_circle_avatar).transform(new GlideCircleTransform(context))
                    .into(img);
        } else {
            img.setImageResource(R.mipmap.icon_circle_avatar);
        }
        int pa = context.getResources().getDimensionPixelOffset(R.dimen.item_list_large_margin_top);
//        if (TextUtils.isEmpty(aDynamicsItem.getContentVideo()) && !ListUtils.isListValued(aDynamicsItem.getContentImage())) {
//            intro.setPadding(pa, 0, pa, pa);
//        } else {
//            intro.setPadding(pa, 0, pa, 0);
//        }
        if (TextUtils.isEmpty(aDynamicsItem.getContentVideo())) {
            play.setVisibility(View.GONE);
            coverImage.setVisibility(View.GONE);
            contentImage = aDynamicsItem.getContentImage();
            if (ListUtils.isListValued(contentImage)) {

                initView(contentImage);
                pic_list.setVisibility(View.VISIBLE);
                video_rl.setVisibility(View.GONE);

            } else {
                pic_list.setVisibility(View.GONE);

            }
        } else {
            pic_list.setVisibility(View.GONE);
            video_rl.setVisibility(View.VISIBLE);
            coverImage.setVisibility(View.VISIBLE);
            play.setVisibility(View.VISIBLE);
            String coverImageUrl = aDynamicsItem.getVideoImage().getImageUrl();
//            DrawableRequestBuilder<String> drb=
            GlideUtil.getGlideWithMid169DefNoFit(context, coverImageUrl)
//                            .fitCenter()
//                            .listener(lis)
                    .into(coverImage);

        }
    }

    @Override
    protected void initInject() {
        ButterKnife.bind(this, itemView);
    }

    private void initView(ArrayList<ContentImage> dataImgs) {
        ArrayList<ImageUr> imgs = new ArrayList<>();
        for (ContentImage contentImage : dataImgs) {
            if (contentImage != null && !TextUtils.isEmpty(contentImage.getImageUrl())) {
                ImageUr imageUr = new ImageUr();
                imageUr.setImageUrl(contentImage.getImageUrl());
                imgs.add(imageUr);
            }
        }
        int pa = context.getResources().getDimensionPixelOffset(R.dimen.item_list_margin_bottom);
//        if (list.size() == 1) {
//            video_list.setPadding(0, 0, 0, pa);
//        } else {
//        pic_list.setPadding(pa, 0, pa, pa);
//        }
        LinearLayoutManager manager = new LinearLayoutManager(context, LinearLayoutManager.HORIZONTAL, false);
        pic_list.setLayoutManager(manager);
        pic_list.setItemAnimator(new DefaultItemAnimator());
        if (picAdapter == null) {
            // new UiListMarginUtils().setMargin(context, pic_list, R.dimen.item_list_large_margin_left, R.dimen.item_list_large_margin_right);
            picAdapter = new HomePicAdapter(context, imgs);
        } else {
            picAdapter.setData(imgs);
        }
        pic_list.setAdapter(picAdapter);

    }

    @OnClick(value = {R.id.item_anchor_dynamics_home_more})
    public void MoreClick(View view) {
        if (null != listener) {
            listener.OnAnchorSelectMore();
        }
    }

    @OnClick(value = {R.id.item_anchor_dynamics_home_page_rl})
    public void onClick(View view) {
      if(null!=itemListener){
          itemListener.OnAnchorSelectItem(dyTemp);
      }

    }

    public interface OnAnchorSelectMoreListener {
        void OnAnchorSelectMore();
    }
    public interface OnAnchorSelectItemListener {
        void OnAnchorSelectItem(ADynamicsItem dyTemp);
    }
}
